﻿var goodtype = "";
$(function () {
    //Init();
    //日期选择
    $(":radio").click(function () {
        if ($(this).val() == "其他") {
            $("#memo").show();
        } else {
            $("#memo").hide();
        }
        goodtype = $(this).val();
    });
    //如果当前时间已经是16:30之后，则显示明天时间
    // var time = new Date();
    // time.setHours("16");
    // time.setMinutes("30");
    // time.setSeconds("0");
    // if (new Date() > time) {
    //     document.getElementById("FetchTime").value = (new Date).getFullYear() + "-" + ((new Date).getMonth() + 1) + "-" + ((new Date).getDate()+1);
    // } else {
    //     document.getElementById("FetchTime").value = (new Date).getFullYear() + "-" + ((new Date).getMonth() + 1) + "-" + ((new Date).getDate());
    // }
    //
    //初始化地区选择器
    var $citypicker1 = $('#sendFullAddress');
    var $citypicker2 = $('#ReceiveFullAddress');

    //将当前页面的标题banner高亮显示
    $(".navList ul li").eq(1).find("a").css("color", "#ed6900");

    //点击提交订单
    $("#orderSubmit").click(function () {
        //表单验证  寄件人信息验证
        if ($("input[name='senderName']").val() == undefined || $("input[name='senderName']").val() == "") {
            layer.msg("请填写寄件人姓名");
            $("input[name='senderName']").focus();
            return false;
        }
        if (!/(^[\u4E00-\u9FA5]+$)|(^[\u4E00-\u9FA5]*·[\u4E00-\u9FA5]*$)/.test($("input[name='senderName']").val())) {
            layer.msg("寄件人姓名格式不正确");
            $("input[name='senderName']").focus();
            return false;
        } 
        if ($("input[name='senderMobile']").val() == "" && $("input[name='senderLandlineNumber']").val() == "") {
            layer.msg("手机号码和座机号码必须填写一个");
            $("input[name='senderMobile']").focus();
            return false;
        }
        if ($("input[name='senderMobile']").val().length > 11) {
            layer.msg("您输入的手机号码不正确");
            $("input[name='senderMobile']").focus();
            return false;
        }
        if ($("input[name='senderMobile']").val() && !$("input[name='senderMobile']").val().match(/^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|(19[0-9]{1}))+\d{8})$/)) {
            layer.msg("请输入有效的手机号码");
            $("input[name='senderMobile']").focus();
            return false;
        }

        // if (($("input[name='senderLandlineNumber']").val() &&
        //     !$("input[name='senderLandlineNumber']").val().match(/^0[\d]{2,3}-[\d]{7,8}(-[\d]{4,4})?$/)) &&
        //     ($("input[name='senderLandlineNumber']").val() &&
        //         !$("input[name='senderLandlineNumber']").val().match(/^400(\d{7}|-\d{4}-\d{3})$/))) {
        //     layer.msg("寄件人座机号码输入错误");
        //     $("input[name='senderLandlineNumber']").focus();
        //     return false;
        // }

        if ($("input[name='senderLandlineNumber']").val() &&
            (!$("input[name='senderLandlineNumber']").val().match(/^0[\d]{2,3}-[\d]{7,8}(-[\d]{4,4})?$/)) &&
            (!$("input[name='senderLandlineNumber']").val().match(/^400(\d{7}|-\d{4}-\d{3})$/))){
            layer.msg("寄件人座机号码输入错误");
           $("input[name='senderLandlineNumber']").focus();
            return false;
        }


        if ($("input[name='sendFullAddress']").val() == "") {
            layer.msg("请选择寄件人地址");
            $("input[name='sendFullAddress']").focus();
            return false;
        }

        if ($("input[name='sendFullAddress']").val().split("/").length != 3){
            layer.msg("请选择寄件人地址");
            $("input[name='sendFullAddress']").focus();
            return false;
        }

        if ($("input[name='sendAddress']").val() == "") {
            layer.msg("请填写寄件人详细地址");
            $("input[name='sendAddress']").focus();
            return false;
        }

        if($("input[name='senderEmail']").val() == "" || $("input[name='senderEmail']").val() == "undefined"){
            layer.msg("请填写邮箱");
            $("input[name='senderEmail']").focus();
            return false;
        }

        if(!$("input[name='senderEmail']").val().match(/^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/)){
            layer.msg("请输入有效的邮箱");
            $("input[name='senderEmail']").focus();
            return false;
        }

        // !/^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/.test($("input[name='senderEmail']").val())

        if ($("select[name='paymentMethod']").val() == "请选择付款方式") {
            layer.msg("请选择付款方式");
            $("select[name='paymentMethod']").focus();
            return false;
        }

        //收件人信息验证
        if ($("input[name='recipientsName']").val() == undefined || $("input[name='recipientsName']").val() == "") {
            layer.msg("请填写收件人姓名");
            $("input[name='recipientsName']").focus();
            return false;
        }
        if (!/(^[\u4E00-\u9FA5]+$)|(^[\u4E00-\u9FA5]*·[\u4E00-\u9FA5]*$)/.test($("input[name='recipientsName']").val())) {
            layer.msg("收件人姓名格式不正确");
            $("input[name='recipientsName']").focus();
            return false;
        } 
        if ($("input[name='recipientsMobile']").val() == "" && $("input[name='recipientsLandlineNumber']").val() == "") {
            layer.msg("收件人手机号码和座机号码必须填写一个");
            $("input[name='recipientsMobile']").focus();
            return false;
        }
        if ($("input[name='recipientsMobile']").val().length > 11) {
            layer.msg("您输入的手机号码不正确");
            $("input[name='recipientsMobile']").focus();
            return false;
        }
        if ($("input[name='recipientsMobile']").val() && !$("input[name='recipientsMobile']").val().match(/^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|(19[0-9]{1}))+\d{8})$/)) {
            layer.msg("请输入有效的手机号码");
            $("input[name='ReceiveMobile']").focus();
            return false;
        }

        if (($("input[name='recipientsLandlineNumber']").val() &&
            !$("input[name='recipientsLandlineNumber']").val().match(/^0[\d]{2,3}-[\d]{7,8}(-[\d]{4,4})?$/)) &&
            (!$("input[name='recipientsLandlineNumber']").val().match(/^400(\d{7}|-\d{4}-\d{3})$/))) {
            layer.msg("收件人座机号码输入错误");
            $("input[name='recipientsLandlineNumber']").focus();
            return false;
        }


        if ($("input[name='recipientsFullAddress']").val() == "") {
            layer.msg("请选择收件人地址");
            $("input[name='recipientsFullAddress']").focus();
            return false;
        }

        if ($("input[name='recipientsFullAddress']").val().split("/").length != 3){
            layer.msg("请选择收件人地址");
            $("input[name='recipientsFullAddress']").focus();
            return false;
        }


        if ($("input[name='recipientsAddress']").val() == "") {
            layer.msg("请填写收件人详细地址");
            $("input[name='recipientsAddress']").focus();
            return false;
        }
        if ($("input[name='itemType']").val() == "" || $("input[name='itemType']").val() == undefined) {
            layer.msg("请选择物品类型");
            return false;
        }
        //验证输入重量必须确保是数字
        if (!$("#itemWeight").val().match("^[0-9]*$")) {
            layer.msg("物品重量请输入0-9的数字");
            return false;
        }

        if (!$("#itemWeight").val()) {
            layer.msg("请输入物品重量!不得大于50kg或小于0kg!");
            return false;
        }

        if ($("#itemWeight").val() > 50 || $("#itemWeight").val() < 0) {
            layer.msg("重量不得大于50kg或小于0kg");
            return false;
        }

        if (goodtype == "" || goodtype == undefined) {
            layer.msg("请选择物品类别");
            return false;
        }
        var content;
        if (goodtype == "其他") {
            if ($('#memo').val() == "") {
                layer.msg("如选择其他，请手动填写物品名称");
                return false;
            } else {
                content = "其他:" + $('#memo').val()
                $("#elseDeliveryGoods").val(content);
            }
        } 
        //预约时间验证

    

        // 获取表单数据
        var serializeForm = $("#orderForm").serialize();
        // 记录阴影弹窗的索引，确保在必要时可以顺利获取到它，并对其关闭
        var shadeLayerIndex = 0;
        layer.open({
            content: '您确定下单吗？',
            btn: ['确定', '取消'],
            shadeClose: false,
            yes: function () {
                layer.msg("您已确认下单");
                // 发起请求
                $.ajax({
                    data: serializeForm,
                    url: '/home/expressMailOrder/add',
                    type: "POST",
                    dataType: "json",
                    success: function (result) {
                        if (result.code == 0) {
                            layer.close(shadeLayerIndex);
                            layer.open({
                                content:"您已成功下单!",
                               time: 1000,
                               end: function () {
                                   layer.msg("您已成功下单，感谢下单!稍后将跳转至首页");
                                   //下单成功跳转到首页页面
                                   setInterval(function () {
                                       window.location.href = "/home/entryPage/index";
                                   }, 1000)
                               }
                            });
                        } else {
                            layer.msg(result.msg);
                        }
                    },
                    error: function (result) {
                        alert("网络错误")
                    },
                    beforeSend: function () {
                        // 提交表单时呈现阴影弹窗
                        shadeLayerIndex = layer.msg("订单提交中...");
                    },
                    complete: function () {
                        // 服务端返回响应信息时将关闭阴影弹窗
                        layer.close(shadeLayerIndex);
                    }
                });   // end ajax
            }, no: function () {
                layer.msg('您选择了取消');
            }
        });
    });

    InitAutoComplete();
});

//输入手机号码带出寄件人地址信息,初始化autocomplete,选中时并赋值给文本框
function InitAutoComplete() {
    //寄件人地址信息
    $("#SenderMobile").autocomplete({
        source: "",
        minLength: 11,
        select: function (event, ui) {
            //此处因手机号是本身输入的，所以select本身就已经赋值了，在后台value给定输入的值就可以了
            //$("#SenderMobile").val(ui.item.label.split("-")[0]);
            $("#SenderName").val(ui.item.label.split("-")[1]);
            $("#sendFullAddress").val(ui.item.label.split("-")[2] + "/" + ui.item.label.split("-")[3] + "/" + ui.item.label.split("-")[4]);
            $("#SendFullAddressDetail").val(ui.item.label.split("-")[5]);
        }
    });
    //收件人地址信息
    $("#ReceiveMobile").autocomplete({
        source: "",
        minLength: 11,
        select: function (event, ui) {
            //$("#SenderMobile").val(ui.item.label.split("-")[0]);
            $("#ReceiveName").val(ui.item.label.split("-")[1]);
            $("#ReceiveFullAddress").val(ui.item.label.split("-")[2] + "/" + ui.item.label.split("-")[3] + "/" + ui.item.label.split("-")[4]);
            $("#ReceiveFullAddressDetail").val(ui.item.label.split("-")[5]);
        }
    });
}

function Show() {
    layer.open({
        title: '申通快递服务协议',
        type: 1,
       area: ['780px', '570px'],
        shadeClose: true, //点击遮罩关闭
        content: $("#agreement")
    })
}


function func() {
    //获取被选中的option标签
    var vs = $('select  option:selected').val();
    if (vs == "月结")
    {
        $('#MonthCustomerDiv').css({ display: 'block' });
    }else
    {
        $('#MonthCustomerDiv').css({ display: 'none' });
    }

}
var typeindex;
function Fastentry(type)
{
    typeindex = type;
    layerindex = layer.open({
        title: '地址智能识别',
        type: 1,
        area: ['560px', '230px'],
        shadeClose: false, //点击遮罩关闭
        content: $("#Fastentry"),
        end: function () {
            $('.Fastentry').css({ display: 'none' });
        }
    })
}

function Fastbtn() {
    // 
    var content = $("#Fastentrycontent").val();
    if (content == "") {
        layer.msg("请输入解析文本");
        return false;
    }
    var yes = 0;
    //if ($('#DeliveryGoods').is(':checked')) {
    //    yes = 1;
    //}
    $.ajax({
        data: {
            content: content,
            IsAdd: yes,
            type: typeindex
        },
        url: "/Order/IntelligentRecognition",
        type: "POST",
        dataType: "json",
        success: function (result) {
            if (result.Status) {
                layer.msg('解析成功');
                //     $('.popupHe2').css({ display: 'none' });
                if (typeindex == "1")
                    {
                $("#SenderName").val(result.Data.name);
                $("#SenderMobile").val(result.Data.phone);
                $("#sendFullAddress").val(result.Data.province +"/"+ result.Data.city +"/"+ result.Data.district);
                $("#SendFullAddressDetail").val(result.Data.address);
                } else if (typeindex=="2")
                {
                    $("#ReceiveName").val(result.Data.name);
                    $("#ReceiveMobile").val(result.Data.phone);
                    $("#ReceiveFullAddress").val(result.Data.province + "/" + result.Data.city + "/" + result.Data.district);
                    $("#ReceiveFullAddressDetail").val(result.Data.address);
                }
                layer.close(layerindex);
                $("#Fastentrycontent").val('');
            } else {
                layer.msg(result.StatusMessage);
            
            }
        },
    });   // end ajax
}
//返回日期的最小值
function minDate() {
    var time = new Date();
    time.setHours("16");
    time.setMinutes("30");
    time.setSeconds("0");
    if (time > new Date()) {
        // alert((new Date).getFullYear() + "-" + ((new Date).getMonth() + 1) + "-" + ((new Date).getDate()));
        return (new Date).getFullYear() + "-" + ((new Date).getMonth() + 1) + "-" + ((new Date).getDate());
    } else {
        // alert((new Date).getFullYear() + "-" + ((new Date).getMonth() + 1) + "-" + ((new Date).getDate() + 1));
        return (new Date).getFullYear() + "-" + ((new Date).getMonth() + 1) + "-" + ((new Date).getDate() + 1);
    }
}
//返回日期的最大值
function maxDate() {
    var time = new Date(minDate());
    //d.setDate(d.getDate() - 2);
    //alert(time);
    time.setDate(time.getDate() + 2);
    //alert(time);
    return time;
}
//返回时间的最小值
function minTime() {
    var minTime = new Date();
    minTime.setHours("10");
    minTime.setMinutes("0");
    minTime.setSeconds("0");
    //alert(time);
    var maxTime = new Date();
    maxTime.setHours("16");
    maxTime.setMinutes("30");
    maxTime.setSeconds("0");
    if (minTime > new Date() || maxTime < new Date()) {
        //alert(time);
        return "10:00:00";
    } else {
        // alert((new Date).getHours() + ":" + (new Date).getMinutes() + ":" + (new Date).getSeconds());
        return (new Date).getHours() + ":" + (new Date).getMinutes() + ":" + (new Date).getSeconds();
    }


}

